/*
 * @author java_deng
 * @date 2025/2/5 11:46
 * @description
 */
package org.hbwl.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.hbwl.dto.Post;
import org.hbwl.dto.Region;
import org.hbwl.mapper.PostMapper;
import org.hbwl.mapper.RegionMapper;
import org.hbwl.po.PostPo;
import org.hbwl.utils.PageResponseData;
import org.hbwl.vo.AnimalVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PostService {
    @Autowired
    private PostMapper postMapper;
    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private RegionService regionService;
    public PageResponseData<Post> list(PostPo post) {
        if(post.getRegionId()!=null) {
            Long addressId = post.getRegionId().get(2);
            if (addressId == 0) {
                addressId = post.getRegionId().get(1);
            }
            post.setAddressId(addressId);
        }
        post.setStatus(3);
        Page<Post> result = PageHelper.startPage(post.getPageIndex(), post.getPageSize());
        postMapper.list(post);
        PageResponseData<Post> pageResponseData= new PageResponseData<>(result.getResult(),result.getTotal(),result.getPages());
        return pageResponseData;
    }

    public void update(Post post) {
        if(post.getRegionId()!=null){
            Long addressId = post.getRegionId().get(2);
            if(addressId==0){
                addressId=post.getRegionId().get(1);
            }
            post.setAddressId(addressId);
            Region region = regionMapper.selectById(addressId);
            post.setAddress(region.getRegionName());
            String fullAddress=regionService.getFullAddress(region);
            post.setFullAddress(fullAddress);
        }
        post.setStatus(1);
        postMapper.updateById(post);
    }

    public void add(Post post) {
        Long addressId = post.getRegionId().get(2);
        if(addressId==0){
            addressId=post.getRegionId().get(1);
        }
        post.setAddressId(addressId);
        Region region = regionMapper.selectById(addressId);
        post.setAddress(region.getRegionName());
        String fullAddress=regionService.getFullAddress(region);
        post.setFullAddress(fullAddress);
        post.setStatus(1);
        postMapper.insert(post);
    }

    public Post get(Long id) {
        return postMapper.detail(id);
    }

    public void delete(Long id) {
        postMapper.deleteById(id);
    }

    public PageResponseData<Post> managerList(PostPo post) {
        Page<Post> result = PageHelper.startPage(post.getPageIndex(), post.getPageSize());
        postMapper.list(post);
        PageResponseData<Post> pageResponseData= new PageResponseData<>(result.getResult(),result.getTotal(),result.getPages());
        return pageResponseData;
    }

    public void audit(Post post) {
        postMapper.updateById(post);
    }
}
